
"""
    菜单
"""

from users.models import Menu
from users.serialiaers import menu
from utils.model_view_set import CustomModelViewSet
from utils.tree import TreeAPIView, TreeAPIFormView


class MenuModelView(TreeAPIFormView, CustomModelViewSet):
    """
    菜单管理：增删改查
    """
    perms_map = ({'*': 'admin'}, {'*': 'menu_all'}, {'get': 'menu_list'}, {'post': 'menu_create'}, {'put': 'menu_edit'},
                 {'delete': 'menu_delete'})
    queryset = Menu.objects.all()
    serializer_class = menu.MenuModelListSerializer
    search_fields = ('name',)  # 已在全局设置：指定字段模糊搜索
    ordering_fields = ('sort',)  # 已在全局设置：指定字段排序

    def get_serializer_class(self):
        # 根据请求类型动态变更serializer
        if self.action == 'retrieve':
            return menu.MenuModelRetrieveSerializer
        return menu.MenuModelListSerializer


class MenuModelTreeView(TreeAPIView):
    """
    菜单树
    """
    queryset = Menu.objects.all()
